package com.hxd.otc.module.home.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.convenience.bean.ConvenienceBannerBean;
import com.hxd.otc.module.convenience.model.ConvenienceModel;
import com.hxd.otc.module.home.contract.HomePageContract;
import com.hxd.otc.module.home.model.MessageModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;

import java.util.ArrayList;

/**
 * Created by lzy on 2018/12/14 0014.
 * description ${便民服务Presenter}
 */
public class HomePagePresenter extends BasePresenter<HomePageContract.IView> implements HomePageContract.IPresenter {

    private final ConvenienceModel mConvenienceModel;
    private final MessageModel mMessageModel;

    public HomePagePresenter(HomePageContract.IView iView) {
        super(iView);
        mConvenienceModel = ConvenienceModel.getInstance();
        mMessageModel = MessageModel.getInstance();
    }

    @Override
    public void getBannerList() {
        mConvenienceModel.getBannerList(2, new BaseObserver<ArrayList<ConvenienceBannerBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<ConvenienceBannerBean> convenienceBannerBeans) {
                mView.getBannerListSuccess(convenienceBannerBeans);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.getBannerListFail(errMessage);
            }
        }, mView.<BaseBean<ArrayList<ConvenienceBannerBean>>>bindAutoDispose());
    }

    @Override
    public void bannerUpdate(String bannerId) {
        mConvenienceModel.bannerUpdate(bannerId, new BaseObserver<Object>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Object o) {
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
            }
        }, mView.<BaseBean<Object>>bindAutoDispose());
    }

    @Override
    public void getMessageNoReadCount() {
        BaseObserver<Integer> baseObserver = new BaseObserver<Integer>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull Integer number) {
                mView.updateMessageRed(number);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.updateMessageRed(null);
            }
        };
        baseObserver.setPostTokenMissEvent(false);
        mMessageModel.getMessageNoReadCount(baseObserver, mView.<BaseBean<Integer>>bindAutoDispose());
    }

    @Override
    public void updateMessageStatus() {
        mMessageModel.updateMessageStatus(new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.updateMessageStatusSuccess();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }
}
